<script setup>
import { ref, onMounted } from 'vue'

// 轮播图数据
const banners = ref([
  {
    id: 1,
    image: 'https://img.alicdn.com/imgextra/i2/O1CN01Vj95n91Lqg9t9l2xK_!!6000000001441-2-tps-1920-600.png',
    link: '#'
  },
  {
    id: 2,
    image: 'https://img.alicdn.com/imgextra/i1/O1CN01O7mG4g1g9FwY56KX4_!!6000000004152-2-tps-1920-600.png',
    link: '#'
  },
  {
    id: 3,
    image: 'https://img.alicdn.com/imgextra/i3/O1CN013XxO5m21eYQp2uJ4z_!!6000000006971-2-tps-1920-600.png',
    link: '#'
  }
])

// 商品分类数据
const categories = ref([
  { id: 1, name: '女装', icon: '👗' },
  { id: 2, name: '男装', icon: '👔' },
  { id: 3, name: '鞋靴', icon: '👟' },
  { id: 4, name: '箱包', icon: '👜' },
  { id: 5, name: '数码', icon: '📱' },
  { id: 6, name: '家电', icon: '📺' },
  { id: 7, name: '家居', icon: '🏠' },
  { id: 8, name: '美妆', icon: '💄' },
  { id: 9, name: '母婴', icon: '👶' },
  { id: 10, name: '食品', icon: '🍔' }
])

// 热门商品数据
const hotProducts = ref([
  // 女装类
  {
    id: 1,
    name: '夏季时尚连衣裙',
    category: '女装',
    price: 199,
    originalPrice: 299,
    image: 'https://img.alicdn.com/imgextra/i1/O1CN012C0rPz1w0GjY6l8tZ_!!6000000006036-2-tps-1000-1000.jpg',
    sales: 1234
  },
  {
    id: 2,
    name: '女士纯棉T恤',
    category: '女装',
    price: 89,
    originalPrice: 129,
    image: 'https://img.alicdn.com/imgextra/i4/O1CN01zX6eP01u4fGxGz5eQ_!!6000000006363-2-tps-1000-1000.jpg',
    sales: 2345
  },
  {
    id: 9,
    name: '女士高腰休闲裤',
    category: '女装',
    price: 159,
    originalPrice: 229,
    image: 'https://img.alicdn.com/imgextra/i1/O1CN01D7hQZg1N3n2Xv5VfB_!!6000000000443-2-tps-1000-1000.jpg',
    sales: 9012
  },
  {
    id: 13,
    name: '女士时尚外套',
    category: '女装',
    price: 259,
    originalPrice: 359,
    image: 'https://img.alicdn.com/imgextra/i3/O1CN01r0YQ3Z1wU7gXpM5nQ_!!6000000005918-2-tps-1000-1000.jpg',
    sales: 13456
  },
  // 男装类
  {
    id: 3,
    name: '男士商务休闲衬衫',
    category: '男装',
    price: 159,
    originalPrice: 259,
    image: 'https://img.alicdn.com/imgextra/i1/O1CN01uIqKpG1jV0ZvNvM5B_!!6000000005086-2-tps-1000-1000.jpg',
    sales: 3456
  },
  {
    id: 4,
    name: '男士修身牛仔裤',
    category: '男装',
    price: 219,
    originalPrice: 319,
    image: 'https://img.alicdn.com/imgextra/i2/O1CN01E5oVn81n4q3Xr5m5M_!!6000000004836-2-tps-1000-1000.jpg',
    sales: 4567
  },
  {
    id: 10,
    name: '男士时尚夹克',
    category: '男装',
    price: 299,
    originalPrice: 399,
    image: 'https://img.alicdn.com/imgextra/i2/O1CN01P8Z6pM1f8r1Xt5v5B_!!6000000003946-2-tps-1000-1000.jpg',
    sales: 10123
  },
  {
    id: 14,
    name: '男士休闲卫衣',
    category: '男装',
    price: 189,
    originalPrice: 259,
    image: 'https://img.alicdn.com/imgextra/i3/O1CN01T6uX5R1h5n2Xq5V5M_!!6000000004415-2-tps-1000-1000.jpg',
    sales: 14567
  },
  // 鞋靴类
  {
    id: 5,
    name: '女士细跟高跟鞋',
    category: '鞋靴',
    price: 299,
    originalPrice: 399,
    image: 'https://img.alicdn.com/imgextra/i2/O1CN01K5Zq7X1b8r1Xv5V5B_!!6000000003945-2-tps-1000-1000.jpg',
    sales: 5678
  },
  {
    id: 6,
    name: '轻便运动跑步鞋',
    category: '鞋靴',
    price: 399,
    originalPrice: 499,
    image: 'https://img.alicdn.com/imgextra/i4/O1CN01J5zQ7X1c8r1Xs5V5B_!!6000000004001-2-tps-1000-1000.jpg',
    sales: 6789
  },
  {
    id: 11,
    name: '女士夏季凉鞋',
    category: '鞋靴',
    price: 199,
    originalPrice: 269,
    image: 'https://img.alicdn.com/imgextra/i1/O1CN01N5oVn81d8r1Xt5v5B_!!6000000003946-2-tps-1000-1000.jpg',
    sales: 11234
  },
  {
    id: 15,
    name: '男士皮鞋',
    category: '鞋靴',
    price: 359,
    originalPrice: 459,
    image: 'https://img.alicdn.com/imgextra/i3/O1CN01Q5zQ7X1e8r1Xu5V5B_!!6000000004002-2-tps-1000-1000.jpg',
    sales: 15678
  },
  // 箱包类
  {
    id: 7,
    name: '女士真皮手提包',
    category: '箱包',
    price: 359,
    originalPrice: 499,
    image: 'https://img.alicdn.com/imgextra/i2/O1CN01H5Zq7X1a8r1Xv5V5B_!!6000000003944-2-tps-1000-1000.jpg',
    sales: 7890
  },
  {
    id: 8,
    name: '男士商务公文包',
    category: '箱包',
    price: 499,
    originalPrice: 699,
    image: 'https://img.alicdn.com/imgextra/i1/O1CN01L5Zq7X1b8r1Xv5V5B_!!6000000003945-2-tps-1000-1000.jpg',
    sales: 8901
  },
  {
    id: 12,
    name: '女士时尚背包',
    category: '箱包',
    price: 199,
    originalPrice: 299,
    image: 'https://img.alicdn.com/imgextra/i4/O1CN01M5Zq7X1c8r1Xv5V5B_!!6000000003946-2-tps-1000-1000.jpg',
    sales: 12345
  },
  {
    id: 16,
    name: '旅行拉杆箱',
    category: '箱包',
    price: 459,
    originalPrice: 599,
    image: 'https://img.alicdn.com/imgextra/i3/O1CN01O5Zq7X1d8r1Xv5V5B_!!6000000003947-2-tps-1000-1000.jpg',
    sales: 16789
  }
])

// 轮播图当前索引
const currentBannerIndex = ref(0)

// 获取类别对应的图标
const getCategoryIcon = (categoryName) => {
  const category = categories.value.find(cat => cat.name === categoryName)
  return category ? category.icon : '🛍️'
}

// 获取类别默认图片
const getDefaultImage = (categoryName) => {
  const defaultImages = {
    '女装': 'https://img.alicdn.com/imgextra/i1/O1CN012C0rPz1w0GjY6l8tZ_!!6000000006036-2-tps-1000-1000.jpg',
    '男装': 'https://img.alicdn.com/imgextra/i1/O1CN01uIqKpG1jV0ZvNvM5B_!!6000000005086-2-tps-1000-1000.jpg',
    '鞋靴': 'https://img.alicdn.com/imgextra/i2/O1CN01K5Zq7X1b8r1Xv5V5B_!!6000000003945-2-tps-1000-1000.jpg',
    '箱包': 'https://img.alicdn.com/imgextra/i2/O1CN01H5Zq7X1a8r1Xv5V5B_!!6000000003944-2-tps-1000-1000.jpg',
    '数码': 'https://img.alicdn.com/imgextra/i1/O1CN01X0FzNQ27YtU4K2bVQ_!!6000000007990-0-picassoopen.jpg',
    '家电': 'https://img.alicdn.com/imgextra/i4/O1CN01x9KZ0h1zJ1Z8X2ZQ_!!6000000006228-0-picassoopen.jpg',
    '家居': 'https://img.alicdn.com/imgextra/i3/O1CN013vR93t1eZ2wY7Y6qL_!!6000000006001-0-picassoopen.jpg',
    '美妆': 'https://img.alicdn.com/imgextra/i3/O1CN013vR93t1eZ2wY7Y6qL_!!6000000006001-0-picassoopen.jpg',
    '母婴': 'https://img.alicdn.com/imgextra/i3/O1CN013vR93t1eZ2wY7Y6qL_!!6000000006001-0-picassoopen.jpg',
    '食品': 'https://img.alicdn.com/imgextra/i3/O1CN013vR93t1eZ2wY7Y6qL_!!6000000006001-0-picassoopen.jpg'
  }
  return defaultImages[categoryName] || 'https://via.placeholder.com/300?text=商品图片'
}

// 轮播图自动播放
let bannerInterval = null

const startBannerAutoPlay = () => {
  bannerInterval = setInterval(() => {
    currentBannerIndex.value = (currentBannerIndex.value + 1) % banners.value.length
  }, 3000)
}

const stopBannerAutoPlay = () => {
  if (bannerInterval) {
    clearInterval(bannerInterval)
  }
}

onMounted(() => {
  startBannerAutoPlay()
})

// 模拟添加到购物车
const addToCart = (product) => {
  console.log('添加到购物车:', product)
  alert(`已将「${product.name}」添加到购物车`)
  
  // 简单的购物车数据存储模拟
  const cart = localStorage.getItem('cart')
  const cartItems = cart ? JSON.parse(cart) : []
  cartItems.push(product)
  localStorage.setItem('cart', JSON.stringify(cartItems))
}
</script>

<template>
  <div class="shop-home">
    <!-- 主内容区域 -->
    <div class="main-content">
      <div class="container">
        <!-- 左侧分类导航和轮播图区域 -->
        <div class="home-main">
          <!-- 左侧分类导航 -->
          <div class="left-category">
            <ul class="category-list">
              <li v-for="category in categories" :key="category.id" class="category-item">
                <a href="#" class="category-link">
                  <span class="category-icon">{{ category.icon }}</span>
                  <span class="category-name">{{ category.name }}</span>
                  <span class="category-more">›</span>
                </a>
              </li>
            </ul>
          </div>
          
          <!-- 轮播图 -->
          <div class="banner-container" @mouseenter="stopBannerAutoPlay" @mouseleave="startBannerAutoPlay">
            <div class="banner-wrapper">
              <div 
                v-for="(banner, index) in banners" 
                :key="banner.id"
                class="banner-item"
                :class="{ active: index === currentBannerIndex }"
                :style="{ transform: `translateX(${(index - currentBannerIndex) * 100}%)` }"
              >
                <a :href="banner.link" target="_blank">
                  <img :src="banner.image" :alt="`Banner ${index + 1}`" class="banner-image" />
                </a>
              </div>
            </div>
            
            <!-- 轮播图指示器 -->
            <div class="banner-indicators">
              <span 
                v-for="(banner, index) in banners" 
                :key="banner.id"
                class="indicator"
                :class="{ active: index === currentBannerIndex }"
                @click="currentBannerIndex = index"
              ></span>
            </div>
          </div>
        </div>
        
        <!-- 特惠活动区域 -->
        <div class="promo-section">
          <div class="promo-header">
            <h2>限时特惠</h2>
            <div class="countdown">
              <span class="countdown-label">距结束</span>
              <span class="countdown-time">01:59:59</span>
            </div>
          </div>
          <div class="promo-products">
            <div v-for="product in hotProducts.slice(0, 4)" :key="product.id" class="promo-product">
              <div class="product-image">
                <img :src="product.image || getDefaultImage(product.category)" :alt="product.name" />
                <div class="category-badge" :title="product.category">
                  <span class="category-badge-icon">{{ getCategoryIcon(product.category) }}</span>
                  <span class="category-badge-name">{{ product.category }}</span>
                </div>
              </div>
              <div class="product-info">
                <div class="product-price">
                  <span class="price">¥{{ product.price }}</span>
                  <span class="original-price">¥{{ product.originalPrice }}</span>
                </div>
                <div class="product-title">{{ product.name }}</div>
                <div class="product-sales">已售 {{ product.sales }}</div>
                <router-link :to="{ name: 'shop', params: { id: product.shopId || '1' } }" class="shop-link">
                  <span class="shop-name">查看店铺</span>
                </router-link>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 时尚女郎横幅 -->
        <div class="fashion-banner">
          <img 
            src="https://img.alicdn.com/imgextra/i4/O1CN01lKQ67p1YQvJZ0g9wN_!!6000000003085-0-picassoopen.jpg" 
            alt="时尚女郎"
            class="fashion-image"
          />
        </div>
        
        <!-- 热门商品区域 -->
        <div class="products-section">
          <div class="section-header">
            <h2>热门商品</h2>
          </div>
          <div class="products-grid">
            <div v-for="product in hotProducts" :key="product.id" class="product-card">
              <div class="product-image">
                <img :src="product.image || getDefaultImage(product.category)" :alt="product.name" />
                <div class="category-badge" :title="product.category">
                  <span class="category-badge-icon">{{ getCategoryIcon(product.category) }}</span>
                  <span class="category-badge-name">{{ product.category }}</span>
                </div>
                <button class="add-to-cart" @click="addToCart(product)">加入购物车</button>
              </div>
              <div class="product-info">
                <div class="product-title">{{ product.name }}</div>
                <div class="product-price">
                  <span class="price">¥{{ product.price }}</span>
                  <span class="original-price">¥{{ product.originalPrice }}</span>
                </div>
                <div class="product-sales">已售 {{ product.sales }}</div>
                <router-link :to="{ name: 'shop', params: { id: product.shopId || '1' } }" class="shop-link">
                  <span class="shop-name">查看店铺</span>
                </router-link>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.shop-home {
  background-color: #f5f5f5;
  min-height: 100vh;
  width: 100%;
}

.main-content {
  padding-top: 0;
  width: 100%;
}

.home-main {
  display: flex;
  gap: 0;
  margin-bottom: 0;
}

/* 左侧分类导航 */
.left-category {
  width: 190px;
  background-color: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  flex-shrink: 0;
}

.category-list {
  padding: 5px 0;
}

.category-item {
  line-height: 36px;
}

.category-link {
  display: flex;
  align-items: center;
  padding: 0 20px;
  color: #333;
  transition: background-color 0.3s;
  font-size: 14px;
}

.category-link:hover {
  background-color: #f5f5f5;
  color: #ff0036;
}

.category-icon {
  margin-right: 8px;
  font-size: 16px;
}

.category-name {
  flex: 1;
}

.category-more {
  color: #999;
}

/* 轮播图样式 */
.banner-container {
  flex: 1;
  position: relative;
  height: 400px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  width: calc(100% - 190px);
}

.banner-wrapper {
  position: relative;
  height: 100%;
}

.banner-item {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: transform 0.5s ease;
}

.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.banner-indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

.indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.6);
  cursor: pointer;
  transition: background-color 0.3s;
}

.indicator.active {
  background-color: #ff0036;
}

/* 特惠活动区域 */
.promo-section {
  background-color: #fff;
  margin-bottom: 0;
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* 时尚女郎横幅 */
.fashion-banner {
  margin: 20px 0;
  overflow: hidden;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.fashion-banner:hover {
  transform: translateY(-5px);
}

.fashion-image {
  width: 100%;
  height: auto;
  display: block;
  object-fit: cover;
}

.products-section {
  background-color: #fff;
  margin-bottom: 20px;
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.promo-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.promo-header h2 {
  font-size: 24px;
  color: #ff0036;
  font-weight: bold;
}

.countdown {
  display: flex;
  align-items: center;
  gap: 10px;
}

.countdown-label {
  font-size: 14px;
  color: #666;
}

.countdown-time {
  font-size: 16px;
  font-weight: bold;
  color: #ff0036;
}

.promo-products {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 15px;
  width: 100%;
}

.promo-product {
  text-align: center;
}

.promo-product .product-image {
  height: 150px;
  margin-bottom: 10px;
  position: relative;
  overflow: hidden;
}

.promo-product .product-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.product-price {
  margin-bottom: 5px;
}

.price {
  font-size: 18px;
  color: #ff0036;
  font-weight: bold;
  margin-right: 5px;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.product-title {
  font-size: 14px;
  color: #333;
  margin-bottom: 5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-sales {
  font-size: 12px;
  color: #999;
}

.shop-link {
  text-decoration: none;
  color: #1989fa;
  font-size: 12px;
  margin-top: 5px;
  display: inline-block;
}

.shop-link:hover {
  color: #409eff;
}

/* 热门商品区域 */
.products-section {
  background-color: #fff;
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.section-header {
  margin-bottom: 20px;
}

.section-header h2 {
  font-size: 20px;
  color: #333;
  font-weight: bold;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  width: 100%;
}

.product-card {
  transition: transform 0.3s, box-shadow 0.3s;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.product-card .product-image {
  position: relative;
  height: 200px;
  margin-bottom: 10px;
  overflow: hidden;
}

.product-card .product-image img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: transform 0.3s;
}

.product-card:hover .product-image img {
  transform: scale(1.05);
}

.add-to-cart {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 36px;
  background-color: #ff0036;
  color: white;
  border: none;
  font-size: 14px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.3s;
}

.product-card:hover .add-to-cart {
  opacity: 1;
}

.add-to-cart:hover {
  background-color: #e60029;
}

/* 类别标签样式 */
.category-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 12px;
  padding: 2px 8px;
  font-size: 12px;
  color: #666;
  opacity: 0;
  transition: opacity 0.3s, background-color 0.3s;
  display: flex;
  align-items: center;
  gap: 4px;
  backdrop-filter: blur(2px);
}

.product-card:hover .category-badge {
  opacity: 1;
}

.category-badge:hover {
  background-color: white;
  color: #ff0036;
}

.category-badge-icon {
  font-size: 14px;
}

@media (max-width: 1200px) {
  .products-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .home-main {
    flex-direction: column;
  }
  
  .left-category {
    width: 100%;
  }
  
  .banner-container {
    height: 200px;
  }
  
  .promo-products,
  .products-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
